package com.example.hasee.plant.config;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

import static android.preference.PreferenceManager.getDefaultSharedPreferences;

/**
 * Created by hasee on 2017-02-03.
 */

public class MSharePreference {
    Context context;
    SharedPreferences sharedPreferences;

    public MSharePreference(Context context){
        this.context=context;
        if(sharedPreferences==null){
            sharedPreferences=getDefaultSharedPreferences(context);
        }
    }

//    保存数据
    public static void putString(Context context, String key, String value) {
        SharedPreferences sharedPreferences = getDefaultSharedPreferences(context);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(key, value);
        edit.commit();
    }
//    获取数据
    public static String getString(Context context, String key, String defValue) {
        SharedPreferences sharedPreferences = getDefaultSharedPreferences(context);
        return sharedPreferences.getString(key, defValue);
    }
//    删除数据
    public static void remove(Context context, String key) {
        SharedPreferences sharedPreferences = getDefaultSharedPreferences(context);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.remove(key);
        edit.commit();
    }

    public static void clearAll(Context context){
        SharedPreferences sharedPreferences = getDefaultSharedPreferences(context);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.clear();
        edit.commit();
    }


    public static Map<String,?> getAllData(Context context){
        SharedPreferences sharedPreferences = getDefaultSharedPreferences(context);
        return sharedPreferences.getAll();
    }

}
